import "../../../../scss/activity/collect/user_vote_page.scss";
import $ from "jquery";
import "../../../utils/weui";
import moment from "moment";

import "../../../utils/remove_ad";
import ps from "../../../common/variable";
import Vue from "vue";
import VoteuserJoin from "./voteUserJoin";
import com from "../../../utils/common";
import "../../../utils/cropper";
import wxtool from "../../components/weixin";

let user = wxtool.getWxUserinfo();
$(function () {
  let page = {
    _actid: com.getUrlParam("actid") ? com.getUrlParam("actid") : 0,
    _joinid: com.getUrlParam("joinid") ? com.getUrlParam("joinid") : 0,
    _app: {},
    _appdata: {},

    _init: function () {

      // 获取当前用户参赛信息
      let self = this;
      com.ajax({
        url: "/act/userVote/getJoinerWork",
        data: {
          Actid: self._actid,
          Joinid: self._joinid,
          Openid:user.openid,
        },
        ok: function (data) {
          let temp = data["msg"];
          console.log(data);
          if (temp !== "fail" && typeof temp === "object") {

            if (temp.Catgory == "collect/user_vote") {
              temp.Actid = self._actid;
              temp.StartDate = moment(temp.StartTime).format("YYYY-MM-DD HH:mm");
              temp.EndDate = moment(temp.EndTime).format("YYYY-MM-DD HH:mm");
              console.log(temp);
              self._appdata.partinstatusall = ps;
              self._appdata.partinstatus = self._checkActStatus(data);


              /*填入用户信息*/
              let JoinerMsg = data["extra"];
              console.log(JoinerMsg)

              let joinerDetail = data["WorkDetail"];
              console.log(joinerDetail);

              let WorkDetail = {};
              WorkDetail.WorkName = JSON.parse(JoinerMsg["Other"]).WorkName;
              WorkDetail.WorksDesc = JSON.parse(JoinerMsg["Other"]).JoinSlogan;
              WorkDetail.WorksImg = JoinerMsg.Results;
              WorkDetail.Votes = JoinerMsg.Current;
              $.extend(WorkDetail,joinerDetail)
              self._appdata.WorkDetail = WorkDetail

              $.extend(self._appdata, temp);
              console.log(self._appdata);
              self._loadTemp();
            }else {
              com.alertDialog("类型错误")
            }
          }else {
            com.alertDialog("活动不存在")
          }
        },
      });
    },

    _checkActStatus: function (act) {
      console.log(act)
      let temp = act['msg'];
      let start = moment(temp.StartTime);
      let end = moment(temp.EndTime);
      let now = moment();

      //判断活动是否开始
      if (now.isBefore(start)) {
        return ps.AtActInActiveStart
      }
      //判断活动是否开始
      if (now.isAfter(end)) {
        return ps.AtActInActiveEnd
      }
      // 当用户是参与者进入其他人页面时
      if (act.isJoiner && act.isPageJoin && act.checkPageJoin) {
        return ps.JoinDone
      }
      //判断帮忙者是否参与过活动(帮忙者是参与者)
      if (act.isJoiner && act.isPageJoin && !act.checkPageJoin) {
        return ps.ParterDone
      }
      //判断帮忙者是否参与过活动(帮忙者不是参与者)
      return ps.ParterLook
    },
    _loadTemp: function () {
      let self = this;
      Vue.component("voteuser-join", VoteuserJoin);
      self._app = new Vue({
        el: "#app",
        data: self._appdata,
        methods: {
          // 显示弹窗,并让其显示在中间
          // 点击投票
          CastVote: function () {
              self._takePartInUserVote();
          },
          AlertJoinPop: function () {
            let msg = '<p style="text-align:left;color:#333;">①报名前可先联系商家，确认活动真实性以及记录联系方式哦！<br/>②如发现虚假活动，请在客牛公众号回复我们。</p>';
            layer.open({
              content: msg
              , btn: ['确定', '取消'],
              yes: function () {
                layer.closeAll();
                $('.pop_mask').show();
                com.showCenter($(".pop_user_msg"))
              }
            });
          },
          // 提交报名信息
          UserJoinAct: function (data) {
            let JoinData = {};
            JoinData.Actid = self._actid;
            JoinData.Joinid = self._joinid;
            JoinData.ActivityCat = "collect/user_vote";
            JoinData.Name = data["Name"];
            JoinData.Mobile = data["Mobile"];
            JoinData.Other = data["Other"];
            JoinData.Results = data.JoinWorkImg;
            JoinData.Openid = user.openid;
            com.ajax({
              url: "/act/userVote/joinUserVote",
              method: "post",
              data: JoinData,
              ok: function (data) {
                console.log(data)
                if (data["msg"] == "ok") {
                  self._getJoinByOpenid()
                } else if (data["msg"] == "fail") {
                  com.alertDialog("同一活动不能重复报名")
                }
              }
            })
          },
          // 跳转回活动主页面
          ReturnAll: function () {
            window.location = "./user_vote.html?actid=" + self._actid;
          },
          BackMine:function () {
            self._getJoinByOpenid()
          }
        },
      })
      self._afterTemp();
    },
    _afterTemp: function () {
      let self = this;
      let main = {
        closePop: function () {
          // 关闭弹窗
          $(".pop_mask,.mask").hide();
        },
      };
      $('.pop_mask').click(main.closePop);
      $('.pop_user_msg').click(function (event) {
        event.stopPropagation()
      })

    },

    _getJoinByOpenid: function () {
      let self = this;
      com.ajax({
        url: "/act/userVote/getJoinerDetail",
        data: {
          Openid: user.openid,
          Actid: self._actid,
        },
        ok: function (dat) {
          console.log(dat)
          let joinMsg = dat["res"];
          if (joinMsg["Id"] > 0) {
            window.location = "/dist/activity/collect/user_vote_page.html?actid=" + self._actid + "&joinid=" + joinMsg["Id"];
          } else {
            com.alertDialog("您还未参加此活动");
          }

        }
      })
    },

    // 帮忙者
    _takePartInUserVote: function () {
      let self = this;
      com.ajax({
        url: "/act/userVote/takePartInUserVote",
        data: {
          Openid: user.openid,
          Actid: self._actid,
          Joinid: self._joinid,
          Catgory: "collect/user_vote",
        },
        ok: function (data) {
          if (data['msg'] == "ok") {

            $(".suc_notify,.vote_suc_notify").show();
            setTimeout(function () {
              self._app.WorkDetail.Votes ++
              $('.ticket').hide();
              $("#box").append("<p>你成功投出一票</p>").css("top","2.8rem").find("img").attr("src","/img/activity/collect/vote/popup_box_ticket.png");
              $(".mask").click(function () {
                $(this).hide();
              })
            },1000)
          } else if (data["msg"] == "success"){
            com.alertDialog("当前用户已经砍到底价")
          }else{
            com.alertDialog("您已帮TA投票")
          }

        }
      })
    },
  }
  page._init();
})

